package com.fuyd.pms.background.rest.qiniu;

import com.fuyd.pms.background.qiniu.QiniuTokenManager;
import com.fuyd.pms.dto.Result;
import com.fuyd.pms.dto.ResultCode;
import com.fuyd.pms.dto.v1.QiniuTokenDto;
import com.fuyd.pms.utils.MediaTypes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 七牛相关的api
 * 在Restful风格中，推荐使用http的状态码来表示请求的状态，本程序并未采用该做法。
 * Created by fuyongde on 2016/1/9.
 */
@RestController
@RequestMapping(value = "/api/qiniu")
public class QiniuRestController {

    /**七牛Token管理器**/
    private QiniuTokenManager qiniuTokenManager;

    @Autowired
    public void setQiniuTokenManager(QiniuTokenManager qiniuTokenManager) {
        this.qiniuTokenManager = qiniuTokenManager;
    }

    /**
     * 获取简单上传的token
     * @return
     */
    @RequestMapping(value = "/tokenSimple", method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8)
    public Result tokenSimple(){
        QiniuTokenDto qiniuToken = new QiniuTokenDto();
        qiniuToken.setToken(qiniuTokenManager.upTokenSimple());
        return new Result(ResultCode.CODE_SUCCESS, ResultCode.MSG_SUCCESS, qiniuToken);
    }

    /**
     * 获取覆盖上传的token
     * @param key 图片的key
     * @return
     */
    @RequestMapping(value = "/tokenCover", method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8)
    public Result tokenCover(@RequestParam(value = "key", required = true) String key){
        QiniuTokenDto qiniuToken = new QiniuTokenDto();
        qiniuToken.setToken(qiniuTokenManager.upTokenCover(key));
        return new Result(ResultCode.CODE_SUCCESS, ResultCode.MSG_SUCCESS, qiniuToken);
    }

}
